package com.zhixun.mobile.sys.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import com.boxin.ims.modules.wechat.entity.Lottery;
import com.boxin.ims.modules.wechat.entity.LotteryConfig;


public class MemberUtils {
	public static final String HUIYUANZHONGXIN = "会员中心";
	public static final String LOGIN = "登陆";
	public static final String ZHUCE = "注册";
	public static final String QIANDAO = "签到";
	public static final String QIANDAOSUCCESS = "签到";
	public static final String JIFENGDUIHUAN = "积分兑换";
	public static final String JIFENCHANPING = "积分产品";
	public static final String JIFENDUIHUANSUCCESS = "积分兑换";
	public static final String MIAOSHA = "秒杀";
	public static final String MIAOSHAERROR = "秒杀";
	public static final String MIAOSHASUCCESS = "秒杀";
	public static final String MYMIAOSHA = "我的秒杀";
	public static final String MYJIANGPING = "我的奖品";
	public static final String MYTUIJIAN = "我的推荐";
	public static final String MYDUIHUAN = "我的兑换";
	public static final String YOUHUIJUAN = "优惠券";
	public static final String YOUHUIJUANMESSAGE ="优惠券";
	public static final String MYYOUHUIJUAN ="我的优惠券";
	public static final String EGGS_LOTTERY ="砸金蛋抽奖";
	
	
	//定义提示文字
	
	public static final String FIRSTLOGINUPDATEPASSWORD = "首次登陆,为了您的安全,请尽快修改您的密码！！！"; //首次登陆修改密码提示文字
	
	

	private LotteryConfig config;
	
	/**
	 * 得到会员中心标题
	 */

	public static String memberTitle(String flag) {

		StringBuffer title = new StringBuffer();

		if (flag.equals("wechatMember")) {
			title.append(MemberUtils.HUIYUANZHONGXIN);
			return title.toString();
		}
		if (flag.equals("login")) {
			title.append(MemberUtils.LOGIN);
			return title.toString();
		}
		if (flag.equals("reg")) {
			title.append(MemberUtils.ZHUCE);
			return title.toString();
		}
		if (flag.equals("toSignup")) {
			title.append(MemberUtils.QIANDAO);
			return title.toString();
		}
		if (flag.equals("signupOk")) {
			title.append(MemberUtils.QIANDAOSUCCESS);
			return title.toString();
		}
		if (flag.equals("exchangeScore")) {
			title.append(MemberUtils.JIFENGDUIHUAN);
			return title.toString();
		}
		if (flag.equals("productShow")) {
			title.append(MemberUtils.JIFENCHANPING);
			return title.toString();
		}
		if (flag.equals("exchangeScoreOk")) {
			title.append(MemberUtils.JIFENDUIHUANSUCCESS);
			return title.toString();
		}
		if (flag.equals("seckills")) {
			title.append(MemberUtils.MIAOSHA);
			return title.toString();
		}
		if (flag.equals("seckillsError")) {
			title.append(MemberUtils.MIAOSHAERROR);
			return title.toString();
		}
		if (flag.equals("seckillsOk")) {
			title.equals(MemberUtils.MIAOSHASUCCESS);
			return title.toString();
		}
		if (flag.equals("mySeckills")) {
			title.append(MemberUtils.MYMIAOSHA);
			return title.toString();
		}
		if (flag.equals("myprize")) {

			title.append(MemberUtils.MYJIANGPING);
			return title.toString();
		}
		if (flag.equals("myrecommend")) {
			title.append(MemberUtils.MYTUIJIAN);
			return title.toString();
		} 
		if (flag.equals("myexchange")) {
			title.append(MemberUtils.MYDUIHUAN);
			return title.toString();
		}if (flag.equals("coupon")) {
			title.append(MemberUtils.YOUHUIJUAN);
			return title.toString();
		}
		if (flag.equals("couponMessage")) {
			title.append(MemberUtils.YOUHUIJUANMESSAGE);
			return title.toString();
		}
		if (flag.equals("eggsLottery")) {
			title.append(MemberUtils.EGGS_LOTTERY);
			return title.toString();
		}
		if (flag.equals("mycoupon")) {
			title.append(MemberUtils.MYYOUHUIJUAN);
			return title.toString();
		}
		else {
			title.append(MemberUtils.HUIYUANZHONGXIN);
			return title.toString();
		}

	}
	
	/**
	 * 是否重复提交
	 * @return false 非重复提交
	 * @return true 重复提交
	 */
  public static Boolean isToken (HttpServletRequest request, String tokenName,String token){
	 String  tokenString = "";
	  Boolean b = true;
	  
	  
	  
	  tokenString= SessionMemberUtils.getSessionByName(request,tokenName);
	  
	  
	  if (AssertUtils.isNullOrEmpty(tokenString)) {
		b = false;
		SessionMemberUtils.addSessionValue(request,tokenName, token);
	}
	  else {
		if (!tokenString.equals(token)) {
			b = false;
			SessionMemberUtils.addSessionValue(request,tokenName, token);
			
		}
		else {
			b = true ;
		}
	}
	  
	
	  
	  return b;
  }
  
  
  /**
	 * 获取中奖编码
	 * 
	 * @param lotteryList
	 * @return
	 */
	public  Lottery extractLottery(List<Lottery> lotteryList,LotteryConfig config) {
		// 随机列表
		List<Lottery> randomList = getRandomList(lotteryList);
		// 根据随机列表得到的概率区段
		List<Integer> percentSteps = getPercentSteps(lotteryList);
		// 概率区段的最大值
		int maxPercentStep = percentSteps.get(percentSteps.size() - 1);
		if(maxPercentStep<=0){
			return null;
		}
		// 在概率区段范围内取一个随机数
		int randomStep = new Random().nextInt(maxPercentStep);
		// 中间元素的下标
		int keyIndex = 0;
		int begin = 0;
		int end = 0;
		for (int i = 0; i < percentSteps.size(); i++) {
			if (i == 0) {
				begin = 0;
			} else {
				begin = percentSteps.get(i - 1);
			}
			end = percentSteps.get(i);
			// 判断随机数值是否在当前区段范围内
			if (randomStep > begin && randomStep <= end) {
				keyIndex = i;
				break;
			}
		}
		
		Lottery lottery = randomList.get(keyIndex);	//已抽到的奖
		
		//抽到后再进行两次机率计算，一个是奖品本身的机率，2是合璧的机率
		
		//
		
		int  lastRand = new Random().nextInt(100);
		if(config!=null && config.getPercent()!=null){
			
			if(lastRand <config.getPercent()){
				int myrand = new Random().nextInt(100);//自身随机机率
				if(lottery !=null && myrand < lottery.getPercent()){
					
					return lottery;
				}else{
					return null;
				}
				
			}else{
				return null;
			}
		}else{
			
			
			return lottery; 
		}
		
	}
	

	/**
	 * 获取随机列表
	 * 
	 * @param rlist
	 * @return
	 */
	private  List<Lottery> getRandomList(List<Lottery> rlist) {
		List<Lottery> oldList = new ArrayList<Lottery>(rlist);
		List<Lottery> newList = new ArrayList<Lottery>();
		// 随机排序的老序列中元素的下标
		int randomIndex = 0;
		// 随机排序下标的取值范围
		int randomLength = 0;
		for (int i = 0; i < rlist.size(); i++) {
			// 指向下标范围
			randomLength = oldList.size() - 1;
			// 取值范围元素的个数为多个时，从中随机选取一个元素的下标
			if (randomLength != 0) {
				randomIndex = new Random().nextInt(randomLength);
				// 取值范围元素的个数为一个时，直接返回该元素的下标
			} else {
				randomIndex = 0;
			}
			// 在新的序列当中添加元素，同时删除元素取值范围中的randomIndex下标所对应的元素
			newList.add(oldList.remove(randomIndex));
		}
		return newList;
	}
	
	/**
	 * 获取概率区段[如：10,15,25,30,40,60,75,80,90,95,100]
	 * 
	 * @param rlist
	 * @return
	 */
	private  List<Integer> getPercentSteps(List<Lottery> rlist) {
		List<Integer> percentSteps = new ArrayList<Integer>();
		int percent = 0;
		for (Lottery r : rlist) {
			percent += r.getPercent();
			percentSteps.add(percent);
		}
		return percentSteps;
	}
	
	
	
	/**
	 * 计算过期时间
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = date.getTime() - new Date().getTime();
		return t/(24*60*60*1000);
	}
	
	/**
	 * 生成6位随机数
	 */
	
	public static int  getRandom(){
		  int[] array = {0,1,2,3,4,5,6,7,8,9};
		   Random rand = new Random();
		   for (int i = 10; i > 1; i--) {
		       int index = rand.nextInt(i);
		       int tmp = array[index];
		       array[index] = array[i - 1];
		       array[i - 1] = tmp;
		   }
		   int result = 0;
		   for(int i = 0; i < 6; i++)
		       result = result * 10 + array[i];
		  return result;
	}
	

	
	
	
  
  
}
